from django.db import models

# Create your models here.
from goods.models import SKU
from meiduo.utils.models import BaseModel
from users.models import User, Address


class OrderInfo(BaseModel):
    # 在创建订单的时候作为订单状态的常量使用
    PAY_METHODS_ENUM = {
        "CASH": 1,
        "ALIPAY": 2
    }
    ORDER_STATUS_ENUM = {
        "UNPAID": 1,
        "UNSEND": 2,
        "UNRECEIVED": 3,
        "UNCOMMENT": 4,
        "FINISHED": 5
    }
    PAY_METHOD_CHOICES = ((1, '货到付款'), (2, '支付宝'))
    ORDER_STATUS_CHOICES = ((1, '待支付'), (2, '待发货'), (3, '待收货'), (4, '待评价'), (5, '已完成'), (6, '以取消'))
    # 订单号设置为主键
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name='订单号')
    # 用户信息关联用户表
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='下单用户')
    address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name='收货地址')
    pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES, default=1, verbose_name='付款方式')
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name='订单状态')
    total_count = models.IntegerField(default=1, verbose_name='订单商品总数')
    # decimal字段用来表示金额 最大十位数 小数占两位
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单总金额')
    freight = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='运费')

    class Meta:
        db_table = "tb_order_info"
        verbose_name = '订单基本信息'
        verbose_name_plural = verbose_name


class OrderGoods(BaseModel):
    SCORE_CHOICES = (
        (0, '0分'), (1, '20分'), (2, '40分'), (3, '60分'), (4, '80分'), (5, '100分')
    )
    order = models.ForeignKey(OrderInfo, related_name='skus', on_delete=models.CASCADE, verbose_name='订单')
    sku = models.ForeignKey(SKU, on_delete=models.PROTECT, verbose_name='订单商品')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')
    count = models.IntegerField(default=1, verbose_name='商品数量')
    comments = models.TextField(default='', verbose_name='商品评论')
    score = models.SmallIntegerField(choices=SCORE_CHOICES, default=5, verbose_name='满意度评分')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否完成评价')

    class Meta:
        db_table = "tb_order_goods"
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name
